﻿using Microsoft.TeamFoundation.Client;
using System;
using System.Net;

namespace TFSExceptionHandling.Infrastructure.TFSHandling
{
    public class TfsHandlingHelper
    {
        #region " Attributes "

        private static TfsTeamProjectCollection _tfs;

        #endregion

        #region " Methods "

        //Connect to TFS Project Collection, provide server URL in format http:// ServerName:Port/Collection
        public static TfsTeamProjectCollection ConnectTfs(TfsHandlingConnection tfsHandlingConnection)
        {
            ICredentials iCred = new NetworkCredential(tfsHandlingConnection.UserName, tfsHandlingConnection.Password, tfsHandlingConnection.Domain);

            _tfs = new TfsTeamProjectCollection(new Uri(tfsHandlingConnection.TfsServer), iCred);

            _tfs.EnsureAuthenticated();

            return _tfs;
        }

        //Disconnect to TFS Project Collection, provide server URL in format http:// ServerName:Port/Collection
        public static void DisconnectTfs()
        {
            _tfs.Disconnect();
            _tfs.Dispose();
        }

        #endregion
    }
}
